//
//  MBProgressHUD+Extend.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/8.
//

#import "MBProgressHUD+Extend.h"
#import "UIImage+GIF.h"

@implementation MBProgressHUD (Extend)
///文字+菊花提示，不会自动消失
+(MBProgressHUD *)rb_showMessage:(NSString *)message ToView:(UIView *)view {
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    //设置方框view为该模式后修改颜色才有效果
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    //设置方框view背景色
    hud.bezelView.backgroundColor =[UIColor blackColor];
    hud.label.text=message;
    hud.label.font= RBPingFangBlodFont(15);
    hud.label.textColor = [UIColor whiteColor];
    //设置菊花框为白色
    hud.contentColor = [UIColor whiteColor];
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    return hud;
}
///显示一条提示信息，自动消失提示
+(MBProgressHUD *)rb_showAutoMessage:(NSString *)message ToView:(UIView *)view{
    MBProgressHUD *hud = [self rb_showMessage:message ToView:view RemainTime:1.5 Model:MBProgressHUDModeText];
    hud.label.font= RBPingFangBlodFont(15);
    hud.label.textColor = [UIColor whiteColor];
    // 代表需要蒙版效果
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    //设置方框view背景色
    hud.bezelView.backgroundColor = [UIColor blackColor];
    return hud;
}
+(MBProgressHUD *)rb_showMessage:(NSString *)message ToView:(UIView *)view RemainTime:(CGFloat)time Model:(MBProgressHUDMode)mode{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.mode = mode;
    //设置方框view为该模式后修改颜色才有效果
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    //设置方框view背景色
    hud.bezelView.backgroundColor = [UIColor blackColor];
    hud.label.text = RBStringIsEmpty(message) ?  @"加载中..." : message;
    hud.label.textColor = [UIColor whiteColor];
    hud.contentColor = [UIColor whiteColor];//文字颜色
    hud.label.font = RBPingFangBlodFont(15);
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:time];
    return hud;
}
//加载视图
+(MBProgressHUD *)rb_showGifToView:(NSString *)message{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    //设置方框view为该模式后修改颜色才有效果
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    //设置方框view背景色
    hud.bezelView.backgroundColor = [UIColor blackColor];
    hud.margin = 5;
    UIImageView *gifAniImage = [[UIImageView alloc]init];
    gifAniImage.contentMode = UIViewContentModeScaleAspectFit;
    NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]]pathForResource:@"loaddh" ofType:@"gif"];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
    UIImage *gifImg = [UIImage sd_imageWithGIFData:imageData];
    gifAniImage.image = gifImg;
    hud.customView = gifAniImage;
    hud.label.text = RBStringIsEmpty(message) ?  @"加载中..." : message;
    hud.label.textColor = [UIColor whiteColor];
    hud.contentColor = [UIColor whiteColor];
    hud.label.font = RBPingFangBlodFont(15);
    hud.removeFromSuperViewOnHide = YES;
    return hud;
}
+(void)rb_hideHUDForView:(UIView *)view{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [self hideHUDForView:window animated:YES];
}
+(void)rb_hideHUD{
    UIView  *winView =(UIView*)[UIApplication sharedApplication].delegate.window;
    [self hideHUDForView:winView animated:YES];
    [self hideHUDForView:[self getCurrentUIVC].view animated:YES];
}
//获取当前屏幕显示的viewcontroller
+(UIViewController *)getCurrentWindowVC{
    UIViewController *result = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tempWindow in windows)
        {
            if (tempWindow.windowLevel == UIWindowLevelNormal)
            {
                window = tempWindow;
                break;
            }
        }
    }
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]])
    {
        result = nextResponder;
    }
    else
    {
        result = window.rootViewController;
    }
    return  result;
}
+(UIViewController *)getCurrentUIVC{
    UIViewController  *superVC = [[self class]  getCurrentWindowVC];
    
    if ([superVC isKindOfClass:[UITabBarController class]]) {
        
        UIViewController  *tabSelectVC = ((UITabBarController*)superVC).selectedViewController;
        
        if ([tabSelectVC isKindOfClass:[UINavigationController class]]) {
            
            return ((UINavigationController*)tabSelectVC).viewControllers.lastObject;
        }
        return tabSelectVC;
    }
    return superVC;
}
@end
